package cn.micro.keep.accounts.service.impl;

import cn.micro.keep.accounts.api.Page;
import cn.micro.keep.accounts.common.enums.ShareGroupStatusEnum;
import cn.micro.keep.accounts.common.utils.SecurityUtil;
import cn.micro.keep.accounts.common.utils.UniqueCodeGenerator;
import cn.micro.keep.accounts.entity.AccountDimShareGroup;
import cn.micro.keep.accounts.entity.AccountDimShareGroupUser;
import cn.micro.keep.accounts.entity.AccountOdsShareGroupExitRecord;
import cn.micro.keep.accounts.entity.dto.AccountDimShareGroupDTO;
import cn.micro.keep.accounts.entity.dto.AccountDimShareGroupSearch;
import cn.micro.keep.accounts.entity.dto.BaseDTO;
import cn.micro.keep.accounts.entity.vo.AccountDimShareGroupVO;
import cn.micro.keep.accounts.exception.MicroSysException;
import cn.micro.keep.accounts.mapper.AccountDimShareGroupMapper;
import cn.micro.keep.accounts.mapstruct.AccountDimShareGroupMapstruct;
import cn.micro.keep.accounts.service.AccountDimShareGroupService;
import cn.micro.keep.accounts.service.AccountDimShareGroupUserService;
import cn.micro.keep.accounts.service.AccountDimUserService;
import cn.micro.keep.accounts.service.AccountOdsShareGroupExitRecordService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 分享组信息表 服务实现类
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AccountDimShareGroupServiceImpl extends ServiceImpl<AccountDimShareGroupMapper, AccountDimShareGroup> implements AccountDimShareGroupService {

   private final AccountDimShareGroupUserService shareGroupUserService;

   private final AccountOdsShareGroupExitRecordService shareGroupExitRecordService;


    @Override
    public List<AccountDimShareGroupVO> listByUser(AccountDimShareGroupSearch search) {
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        return this.baseMapper.queryByUser(search);
    }

    @Override
    @DSTransactional
    public boolean create(AccountDimShareGroupDTO dto) {
        boolean result = false;
        Long userId = SecurityUtil.getUserId();
        LambdaQueryWrapper<AccountDimShareGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDimShareGroup::getName,dto.getName())
                .eq(AccountDimShareGroup::getCreatorId,userId);
        AccountDimShareGroup group = this.baseMapper.selectOne(queryWrapper);
        if (group!= null){
            throw new MicroSysException("该分组已存在");
        }
        AccountDimShareGroup shareGroup = new AccountDimShareGroup();
        AccountDimShareGroupMapstruct.INSTANCE.dtoToEntity(dto,shareGroup);
        shareGroup.setCode(UniqueCodeGenerator.generateUniqueCodeByLen(10));
        shareGroup.setCreatorId(userId);
        if (this.save(shareGroup)){
            AccountDimShareGroupUser groupUser = new AccountDimShareGroupUser();
            groupUser.setUserId(userId);
            groupUser.setShareGroupId(shareGroup.getId());
            result = shareGroupUserService.save(groupUser);
        }
        return result;
    }



    @Override
    public boolean dismissGroup(BaseDTO dto) {
        LambdaQueryWrapper<AccountDimShareGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDimShareGroup::getId,dto.getId())
                .eq(AccountDimShareGroup::getCreatorId,SecurityUtil.getUserId());
        AccountDimShareGroup shareGroup = this.getOne(queryWrapper);
        if (shareGroup == null){
            throw new MicroSysException("您没有权限解散该分组");
        }
        shareGroup.setStatus(ShareGroupStatusEnum.DISSOLUTION.getValue());
        return this.updateById(shareGroup);
    }

    @Override
    @DSTransactional
    public boolean exitGroup(Long id) {
        LambdaQueryWrapper<AccountDimShareGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDimShareGroupUser::getShareGroupId,id)
                .eq(AccountDimShareGroupUser::getUserId,SecurityUtil.getUserId());
        if (shareGroupUserService.remove(queryWrapper)){
            AccountOdsShareGroupExitRecord exitRecord = new AccountOdsShareGroupExitRecord();
            exitRecord.setShareGroupId(id);
            exitRecord.setUserId(SecurityUtil.getUserId());
            exitRecord.setExitTime(LocalDateTime.now());
            return shareGroupExitRecordService.save(exitRecord);
        }
        return false;
    }
}
